//
//  PPTopicTableViewCell.m
//  PetParadise
//
//  Created by 陈无忌 on 8/19/14.
//  Copyright (c) 2014 whalefin. All rights reserved.
//

#import "PPTopicTableViewCell.h"
#import "supportingHeadFiles.h"
#import "PPSystemConfiguraton.h"

@implementation PPTopicTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        self.dateLabel.hidden = NO;
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)cellInitWithTopic:(PPTopic *)topic andBeforeTopic:(PPTopic *)beforeTopic
{
    NSDateFormatter *fomatter = [[NSDateFormatter alloc] init];
    [fomatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *thisDate = [fomatter dateFromString:topic.publicDate];
    NSDate *beforeDate;
    if (beforeTopic) {
        beforeDate = [fomatter dateFromString:beforeTopic.publicDate];
    }
    [fomatter setDateFormat:@"ddMM月"];
    NSString *thisDateStr = [fomatter stringFromDate:thisDate];
    NSString *beforeDateStr;
    if (beforeTopic) {
        beforeDateStr = [fomatter stringFromDate:beforeDate];
    }
    
    
    
    if (beforeTopic && [beforeDateStr isEqualToString:thisDateStr]) {
            self.dateLabel.hidden = YES;
    }else{
        self.dateLabel.hidden = NO;
        NSDate *date = [NSDate date];
        
        [fomatter setDateFormat:@"ddMM月"];
        
        if ([thisDateStr isEqualToString:[fomatter stringFromDate:date]]) {
            if (topic.userId == [PPSystemConfiguraton sharedSystemConfiguration].userInfo.id){
                self.dateLabel.hidden = YES;
            }else{
                self.dateLabel.hidden = NO;
                self.dateLabel.text = @"今天";
            }
            
        }else{
            NSString *beforeStr = [thisDateStr substringToIndex:2];
            NSString *afterStr = [thisDateStr substringFromIndex:2];
            if ([afterStr isEqualToString:@"12月"]) {
                afterStr = @"十二月";
            }else if ([afterStr isEqualToString:@"11月"]){
                afterStr = @"十一月";
            }else if ([afterStr isEqualToString:@"10月"]){
                afterStr = @"十月";
            }else if ([afterStr isEqualToString:@"09月"]){
                afterStr = @"九月";
            }else if ([afterStr isEqualToString:@"08月"]){
                afterStr = @"八月";
            }else if ([afterStr isEqualToString:@"07月"]){
                afterStr = @"七月";
            }else if ([afterStr isEqualToString:@"06月"]){
                afterStr = @"六月";
            }else if ([afterStr isEqualToString:@"05月"]){
                afterStr = @"五月";
            }else if ([afterStr isEqualToString:@"04月"]){
                afterStr = @"四月";
            }else if ([afterStr isEqualToString:@"03月"]){
                afterStr = @"三月";
            }else if ([afterStr isEqualToString:@"02月"]){
                afterStr = @"二月";
            }else if ([afterStr isEqualToString:@"01月"]){
                afterStr = @"一月";
            }
            NSString *newDateStr = [NSString stringWithFormat:@"%@%@",beforeStr,afterStr];
            
            self.dateLabel.hidden = NO;
            NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc] initWithString:newDateStr];
            [attriString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:20.0f] range:NSMakeRange(0,2)];
            if (attriString.length == 4) {
                [attriString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:10.0f] range:NSMakeRange(2,2)];
            }else if(attriString.length == 5){
                [attriString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:10.0f] range:NSMakeRange(2,3)];
            }
            
            [self.dateLabel setText:attriString];
        }
    }

    if (topic.topicImgList.count > 0) {
        self.imageCountLabel.hidden = NO;
        self.contentLabel.hidden = NO;
        self.imageShowView.hidden = NO;
        self.topicLabel.hidden = YES;
        //remove subview
        for (UIImageView *subView in self.imageShowView.subviews)
        {
            [subView removeFromSuperview];
        }
        //data set
        if (topic.topicImgList.count > 4) {
            self.imageCountLabel.text = [NSString stringWithFormat:@"共%d张",topic.topicImgList.count];
        }else{
            self.imageCountLabel.hidden = YES;
        }
        [PPImageShowView imageShowViewsetImagePositionWithImage:topic.topicImgList andView:self.imageShowView];
        self.contentLabel.frame = CGRectMake(168, 10, 150, 17);
        self.contentLabel.text = topic.content;
        self.contentLabel.numberOfLines = 0;
        self.contentLabel.lineBreakMode = NSLineBreakByCharWrapping;
        [self.contentLabel sizeToFit];
    }else{
        self.topicLabel.frame = CGRectMake(84, 5, 211, 17);
        self.topicLabel.text = topic.content;
        self.topicLabel.numberOfLines = 0;
        self.topicLabel.lineBreakMode = NSLineBreakByCharWrapping;
        [self.topicLabel sizeToFit];
        
        self.imageCountLabel.hidden = YES;
        self.contentLabel.hidden = YES;
        self.imageShowView.hidden = YES;
        self.topicLabel.hidden = NO;
    }
}

+(CGFloat) getCellHeight:(PPTopic *)topic
{
    if (topic.topicImgList.count > 4) {
        UILabel *content = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 144, 17)];
        content.font = [UIFont systemFontOfSize:14.0];
        content.text = topic.content;
        content.numberOfLines = 0;
        content.lineBreakMode = NSLineBreakByCharWrapping;
        [content sizeToFit];
        CGFloat height = 10 + content.frame.size.height + 2 + 10 + 5;
        return height > 98 ? height:98;
    }else if (topic.topicImgList.count <= 4 && topic.topicImgList.count > 0){
        UILabel *content = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 144, 17)];
        content.font = [UIFont systemFontOfSize:14.0];
        content.text = topic.content;
        content.numberOfLines = 0;
        content.lineBreakMode = NSLineBreakByCharWrapping;
        [content sizeToFit];
        CGFloat height = 5 + content.frame.size.height + 5;
        return height > 86 ? height:86;
    }else{
        UILabel *content = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 211, 17)];
        content.font = [UIFont systemFontOfSize:14.0];
        content.text = topic.content;
        content.numberOfLines = 0;
        content.lineBreakMode = NSLineBreakByCharWrapping;
        [content sizeToFit];
        CGFloat height = 5 + content.frame.size.height + 5;
        return height > 27 ? height:27;
    }
}



@end
